// miniprogram/pages/detail/detail.js
const db = wx.cloud.database()
const app = getApp()
const _ = db.command
var util = require('../../utils/utils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    message:{
      userImg:'',
      userName:'',
      userWx:'',
      Text:'',
      Date:'',
    },
    thisid:'',
    FindTeamType:'zj',
    array: ['求捞', '招募'],
    index: "2",
    currentNum : 0,
    max : 300,
    current　: 'mid',
    listData: [],
    listData_left:[],
    listData_right:[],
    listData_mid: [],
    display:false,
    weixinNumber:'cjand132',
    chooseSize: false,
    animationData: {},
    haveText : false,
    Activity:{},
    type:'',
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        
    const that = this
    db.collection('user').where({
      _openid:'app.globalData.openid' // 填入当前用户 openid
    }).get({
      success:res=>{
        
        that.data.message.userImg =  res.data[0].UserInfo.avatarUrl
        that.data.message.userName =  res.data[0].UserInfo.userName
      }

    })
    const eventChannel = this.getOpenerEventChannel()
    
     // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
     eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log(data) 
      that.data.thisid = data.data._id
      that.data.type = data.data.Activity.type
      that.setData({
        Activity:data.data.Activity,
        listData_left:data.data.publish1,
        listData_mid:data.data.publish2
      },()=>{
        console.log(that.data.Activity)
        console.log(that.data.listData_left)
        console.log(that.data.listData_mid)
      }
      )
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  bindPickerChange: function (e) {
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index
    })
   if(index == 0){
    //  wx.navigateTo({
    //    url: '../Team/zjToken/zjToken',
    //  })
    this.setData({
      chooseSize:true,
      current:'mid'
    })
   }
   else if(index == 1){
    //  wx.navigateTo({
    //    url: '../Team/xcToken/xcToken',
    //  })
     this.setData({
       chooseSize: true,
       current: 'left'
     })
   }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleCurrent(ev) {
    let current = ev.target.dataset.current;
    if (current == this.data.current) {
      return false;
    }
    let listData = [];
    if (current == 'left'||current =='') {
      listData = this.data.listData_left;
    }
    else if (current == 'mid') {
      listData = this.data.listData_mid;
    }
    // else {
    //   // listData = this.data.listData_right;
    //   this.chooseSize();
    // }
    this.setData({
      current: current,
      listData
    });
  },
  gotoTeam:function(){
  wx.switchTab({
    url: '/pages/Team/index/index',
  })

  },
  Display:function(){
    if(display==false){
      display=true;
    }
    else{
      display=false
    }
    },
chooseSize:function(e){
  // 用that取代this，防止不必要的情况发生
  var that = this;
  // 创建一个动画实例
  var animation = wx.createAnimation({
    // 动画持续时间
    duration: 500,
    // 定义动画效果，当前是匀速
    timingFunction: 'linear'
  })
  // 将该变量赋值给当前动画
  that.animation = animation
  // 先在y轴偏移，然后用step()完成一个动画
  animation.translateY(200).step()
  // 用setData改变当前动画
  that.setData({
    // 通过export()方法导出数据
    animationData: animation.export(),
    // 改变view里面的Wx：if
    chooseSize: true
  })
  // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
  setTimeout(function () {
    animation.translateY(0).step()
    that.setData({
      animationData: animation.export()
    })
  }, 200)
},
  hideModal: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(200).step()
    that.setData({
      animationData: animation.export()

    })
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),
        chooseSize: false
      })
    }, 200)
  },

 // 文本框字数限制
  	limit: function (e) {
    var value = e.detail.value;
    var length = parseInt(value.length);
    this.data.message.Text = value
    if (length > this.data.noteMaxLen) {
      return;
    }

    this.setData({
      currentNum: length
    });
  },
  CancelChoose:function(){
    this.setData({
      chooseSize : false
    })
  },
  editWx:function(e){
   
      this.data.message.userWx= e.detail.value
   
  },
  submitMessage:function(){
    const that = this
    const thisid = this.data.thisid
    console.log(thisid)
    var activity = db.collection('AnotherAcitivity')

    if (this.data.type == "体育活动") {
      console.log("tiyu")
      activity = db.collection('SportActivity')

    }
    else if (this.data.type == "学术活动") {
      activity = db.collection('AcademicActivity')
    }
    else if (this.data.type == "艺术活动") {
      activity = db.collection('ArtActivity')
    }
    else {
      activity = db.collection('AnotherAcitivity')
    }

    console.log(this.data.message) 
    this.data.message.Date = util.formatTime(new Date());


    this.data.message.Date =this.data.message.Date.substring(0,10)

    if(this.data.current=='left')
    {
      activity.doc(thisid).update({
      data:{
        publish1:_.push(this.data.message)
      }
    })
    }
    else 
    {
      activity.doc(thisid).update({
      data:{
        publish2:_.push(this.data.message)
      }
    })
    }
    


  this.setData({
    chooseSize : false
  })
  }
})